package com.helloandroid.tools

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import com.helloandroid.MyApplication
import kotlinx.coroutines.delay

object NetWorkUtil {

    @JvmStatic
    fun networkConnected(): Boolean {
        val ctx = MyApplication.app
        val cm = ctx.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val network = cm.activeNetwork
        if (network != null) {
            val nc = cm.getNetworkCapabilities(network)
            if (nc != null) {
                if (nc.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { //WIFI
                    return true
                } else if (nc.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { //移动数据
                    return true
                }
            }
        }
        return false
    }

    suspend fun mockDelay(ms:Long = 100){
        delay(ms)
    }
}